//
//  TableViewController.swift
//  test1
//
//  Created by infitecs on 16/1/3.
//  Copyright © 2016年 lewis. All rights reserved.
//

import UIKit

var textArr : [String] = []

class TableViewController : UIViewController, UITableViewDataSource, UITableViewDelegate {
    
    @IBOutlet weak var tableView: UITableView!
   
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        
        for i in 0...20 {
            textArr.append("text\(i)");
        }
        
        
        weak var weakSelf = self as TableViewController
        self.tableView.headerViewRefreshAnimationStatus(.headerViewRefreshArrowAnimation, images: [])
        // 及时上拉刷新
        tableView.nowRefresh({ () -> Void in
            weakSelf?.delay(2.0, closure: { () -> () in})
            weakSelf?.delay(2.0, closure: { () -> () in
                print("nowRefresh success")
                weakSelf?.tableView.reloadData()
                weakSelf?.tableView.doneRefresh()
            })
        })
        
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int{
        return textArr.count
    }
    
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = self.tableView.dequeueReusableCellWithIdentifier("testCell")! as UITableViewCell
        let label = cell.viewWithTag(100) as! UILabel
        label.text = textArr[indexPath.row]
        return cell
    }
    
    func delay(delay:Double, closure:()->()) {
        dispatch_after(
            dispatch_time(
                DISPATCH_TIME_NOW,
                Int64(delay * Double(NSEC_PER_SEC))
            ),
            dispatch_get_main_queue(), closure)
    }

    
}

